package hoyo.zqy.pet.service.impl;

import hoyo.zqy.basic.service.IBaiduAiAuditService;
import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.LoginContext;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.pet.domain.Pet;
import hoyo.zqy.pet.domain.PetDetail;
import hoyo.zqy.pet.domain.PetOnlineAuditLog;
import hoyo.zqy.pet.mapper.PetDetailMapper;
import hoyo.zqy.pet.mapper.PetMapper;
import hoyo.zqy.pet.mapper.PetOnlineAuditLogMapper;
import hoyo.zqy.pet.service.IPetService;
import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 保存
     * @param pet
     */
    @Override
    public void save(Pet pet) {
        super.save(pet);
        //保存宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    /**
     * 修改
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            //之前在分页查询已经将id查询出来了 - 前端row中有
            petDetailMapper.updatePetDetail(petDetail);
            //注意在修改的时候容易把详情中的pet_id修改为null，导致下一次回显失败。所以在修改宠物详情的时候不要修改宠物id
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        petDetailMapper.delByPetId(id);
        super.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleBatch(Long[] ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public AjaxResult onSale(Long[] ids, HttpServletRequest request) {
        //获取当前登录信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取当前管理员id
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //遍历拿出全部id
        for (Long id : ids) {
            //通过id拿到数据
            Pet pet = petMapper.loadById(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            if (boo1 && boo2){
                //修改时间
                pet.setOnsaletime(new Date());
                //修改状态
                pet.setState(1);
                //保存
                petMapper.update(pet);
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            }else {
                //添加审核日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("完啦，有好康的");
            }

        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(Long[] ids) {
        //遍历拿出全部id
        for (Long id : ids) {
            //通过id拿到数据
            Pet pet = petMapper.loadById(id);
            //修改时间
            pet.setOffsaletime(new Date());
            //修改状态
            pet.setState(0);
            //保存
            petMapper.update(pet);
        }
    }
}
